package com.wl.dzt.basic.controller;

import com.wl.dzt.basic.service.BasicService;
import com.wl.dzt.sys.core.dao.mate.DataMateBase;
import com.wl.dzt.sys.util.CollectionUtil;
import com.wl.dzt.sys.util.WebUtils;
import com.wl.dzt.sys.util.json.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/6/14/014
 * @description 应用控制器
 * 用于初始化一些参数到页面中
 */
@Api(description = "application", tags = "用户基础接口(basic-api)")
@RestController
@RequestMapping("application")
public class ApplicationController {

    /**
     * 基础控制器
     */
    @Resource
    private BasicService basicService;

    /**
     * <b>获取全部表实体</b>
     * <p>
     * 获取全部表实体
     * </p>
     *
     * @param response
     */
    @PostMapping("allEntityClass")
    @ApiOperation("获取全部表实体")
    public void allEntityClass(HttpServletResponse response) {
        List<String> classes = new ArrayList<>();
        DataMateBase.getAllTales().forEach((c, t) -> classes.add(String.format("%s = '%s' ", c.getSimpleName(), c.getName())));
        WebUtils.print(CollectionUtil.join(classes, ";"), response);
    }

    /**
     * <b>获取所有字段及其中文</b>
     * <p>
     * 获取所有字段及其中文
     * </p>
     *
     * @param response
     */
    @PostMapping("allFields")
    @ApiOperation("获取所有字段及其中文")
    public void allFields(HttpServletResponse response) {
        Map<String, Map<String, Object>> fields = new HashMap<>();
        DataMateBase.getAllTales().forEach((c, t) -> {
            String className = c.getSimpleName();
            Map<String, Object> fs = new HashMap<>();
            t.getColumns().forEach((key, field) -> {
                Map<String, Object> data = new HashMap<>();
                data.put("text", field.getText());
                data.put("maxLength", field.getMaxLength() == -1 ? null : field.getMaxLength());
                fs.put(key, data);
            });
            fields.put(className, fs);
        });
        WebUtils.print(Json.parse(fields), response);
    }


}
